﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.Configuration;

using SBPweb.PROvibe.Shared.Contracts.Services;

namespace SBPweb.PROvibe.Server.Framework.Communication
{

    public class BasicServiceHostFactory<TService, TContract> : ServiceHostFactoryBase
        where TService : IService
        where TContract : IService
    {

        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            Uri baseAddress = baseAddresses[0];
            string dnsSafeHost = ConfigurationManager.AppSettings["dnsSafeHost"];
            if (!string.IsNullOrEmpty(dnsSafeHost))
            {
                foreach (Uri uri in baseAddresses)
                {
                    if (dnsSafeHost == uri.DnsSafeHost)
                    {
                        baseAddress = uri;
                        break;
                    }
                }
            }
            return new CustomHost(typeof(TService), typeof(TContract), baseAddress);
        }

    }

    //public class BasicServiceHostFactory<TContract, TService> : ServiceHostFactoryBase
    //{
    //    public override ServiceHostBase CreateServiceHost(string constructorString,
    //        Uri[] baseAddresses)
    //    {
    //        return new BasicServiceHost<TContract, TService>(baseAddresses);
    //    }
    //}

    //public class BasicServiceHost<TContract, TService> : ServiceHost
    //{

    //    public BasicServiceHost(params System.Uri[] addresses)
    //    {
    //        base.InitializeDescription(typeof(TService), new UriSchemeKeyedCollection(addresses));
    //    }

    //    protected override void InitializeRuntime()
    //    {
    //        //BasicHttpBindingElement bindingElement = new BasicHttpBindingElement();
    //        //ServiceEndpoint sep =
    //        //this.AddServiceEndpoint(
    //        //    typeof(TContract),
    //        //    new CustomBinding(
    //        //        bindingElement,
    //        //        new TextMessageEncodingBindingElement(
    //        //            MessageVersion.Soap11,
    //        //            System.Text.Encoding.UTF8),
    //        //        new HttpTransportBindingElement()),
    //        //    "");

    //        // Define the binding and set time-outs
    //        BindingElement bindingElement = new ReliableSessionBindingElement();

    //        ServiceEndpoint sep =
    //        this.AddServiceEndpoint(
    //            typeof(TContract),
    //            new CustomBinding(
    //                bindingElement,
    //                new TextMessageEncodingBindingElement(
    //                    MessageVersion.Soap12,
    //                    System.Text.Encoding.UTF8),
    //                new HttpTransportBindingElement()
    //            ),
    //            "");

    //        //IEndpointBehavior ieb = 
    //        //sep.Behaviors.Add(ieb);

    //        base.InitializeRuntime();
    //    }

    //}

}